package com.hhs.storage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.XmHotkey;
import com.hhs.storage.service.XmHotkeyService;
import com.hhs.storage.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chx
 * @since 2024-10-15
 */
@RestController
@RequestMapping("xmhotkey")
@Slf4j
public class XmHotkeyController {
    @Resource
    private XmHotkeyService xmHotkeyService;
    @RequestMapping("edit")
    public Map<String,Object> edit(XmHotkey xmHotkey){
//        if(StringUtil.iszNull(xmHotkey.getId())){
//            xmHotkeyService.save(xmHotkey);
//        }else{
          QueryWrapper<XmHotkey> queryWrapper = new QueryWrapper<>();
          queryWrapper.eq("hot_key",xmHotkey.getHotKey());
          XmHotkey findXmlHotkey = xmHotkeyService.list(queryWrapper).stream().findFirst().orElse(null);
          if(findXmlHotkey != null){
              xmHotkey=findXmlHotkey;
          }
          int i=xmHotkey.getUseCount()==null?0:xmHotkey.getUseCount();
          i++;
          xmHotkey.setUseCount(i);
          xmHotkeyService.saveOrUpdate(xmHotkey);
//        }
        return ResponseInfo.success();
    }

    @RequestMapping("list")
    public Map<String,Object> list(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("use_count");
        Page page = xmHotkeyService.page(new Page<XmHotkey>(1,20),queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("code",1);
        map.put("rows",page.getRecords());
        map.put("total",page.getTotal());
        return map;
    }
}

